import cryptoJs from "crypto-js";
import {
	encrypt,
	decrypt,
	_getRandomString,
	sign
} from "./encrypter.js"
// export let baseURL = "https://admin.fulezhenxuan.cn/app/" //预上线接口
export let baseURL = "https://api-test.fudiemall.com/" //测试接口
// export let baseURL = "https://api.fudiemall.com/" //正式接口
var domain = baseURL.indexOf("test");
export const envflag = false
//是否开启加密解密
export const aesKey = '61lNjHmxaIDAS5i7'; // 请替换为你的AES密钥 
const aesIV = _getRandomString(16); // 使用随机生成的IV
export const myRequest = (options) => {

	const obj = options.data
	const maskLayer = options.data.mask || 0
	const objjson = encrypt(JSON.stringify(obj), aesKey, aesIV)
	// #ifdef H5
	const sourceclient = "h5"
	// #endif
	// #ifdef MP-WEIXIN
	const sourceclient = "miniapp"
	// #endif
	// #ifdef MP-ALIPAY
	const sourceclient = "aliminiapp"
	// #endif
	return new Promise((resolve, reject) => {
		if (maskLayer) {
			uni.showLoading({
				title: '加载中',
				mask: true
			});
		}
		uni.request({
			url: baseURL + options.url,
			method: options.method || 'GET',
			data: envflag ? {
				encryptedData: aesIV + objjson
			} : obj,
			header: {
				"source-client": sourceclient,
				"ba-user-token": uni.getStorageSync("token") || '',
				'content-type': "application/json",
			},

			success: (res) => {
				// console.log(res);
				if (maskLayer) uni.hideLoading();
				uni.stopPullDownRefresh();
				if (domain == -1) {
					// console.log('正式域名');
					console.log = () => {}; //关闭所有打印
					const encryptedResponse = res.data.encryptedData;
					const data = decrypt(encryptedResponse.slice(16), aesKey, encryptedResponse.slice(0, 16));
					// console.log(data);
					resolve(data);
					if (data.code == 0) {
						uni.$u.toast(data.msg);
					} else if (data.code == 401 || data.code == 409) {
						uni.removeStorageSync('token')
						if (uni.getStorageSync("is_payPage")) {

						} else {
							console.log("我走这里了，去登陆页")
							setTimeout(() => {
								uni.navigateTo({
									url: '/pageLogin/login/login'
								})
							}, 1500)
							uni.$u.toast(data.msg);
						}
					} else if (data.code == 3000) {
						uni.$u.toast(data.msg);
						uni.removeStorageSync('token')
						setTimeout(() => {
							uni.reLaunch({
								url: '/pageLogin/login/notice'
							})
						}, 1500)
					}
				} else {
					// console.log('测试域名');
					// console.log = () => {};//关闭所有打印
					console.log(res);
					resolve(res.data);
					if (res.data.code == 0) {
						uni.$u.toast(res.data.msg);
					} else {
						if (res.data.code == 401 || res.data.code == 409) {
							
							uni.removeStorageSync('token')
							if (uni.getStorageSync("is_payPage")) {
							console.log("有参数走这里啊")
							} else {
								console.log("我走这里了，去登陆页")
								setTimeout(() => {
									uni.navigateTo({
										url: '/pageLogin/login/login'
									})
								}, 1500)
								uni.$u.toast(data.msg);
							}
					
						} else if (res.data.code == 3000) {
							uni.removeStorageSync('token')
							setTimeout(() => {
								uni.reLaunch({
									url: '/pageLogin/login/notice'
								})
							}, 1500)
						}
					}
				}
			},
			fail: (err) => {
				uni.$u.toast('服务器请求失败')
				reject(err);
			}
		});
	});
};